//
//  BuilsServerDetailsViewController.m
//  InviteJob
//
//  Created by chirui on 2017/10/30.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "BuilsServerDetailsViewController.h"
#import "ReportNoMiddleTVC.h"
#import "TCLPhotoBrowserViewController.h"

@interface BuilsServerDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    UITableView * _tableView;
    float heardHeight;
    UIView * bageView;
    NSDictionary * dic;
    
    ReportNoMiddleTVC * reportVc;
   
    UIButton *buttonPay;
}

@property (nonatomic, strong) SZTextView *contentTextView;

@end

@implementation BuilsServerDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
     TitleView(@"服务详情");
 
    [self creatTableView];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem rightItemWithTitle:@"举报" target:self action:@selector(reportBtn)];
    
    [self getDetailsRequest];
}

-(void)reportBtn{
    isUser
    if (!user.user_id) {
        SHuiMBhudTextTimeStr(@"请先登录");
    }
    
    [self roomeReportView];
}

-(void)creatTableView{
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStyleGrouped];
    _tableView.dataSource =self;
    _tableView.delegate =self;
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    buttonPay = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image = [UIImage imageNamed:@"img_login_button"];
    [buttonPay setTitle:@"查看联系方式" forState:UIControlStateNormal];
    buttonPay.titleLabel.font = [UIFont systemFontOfSize:16];
    buttonPay.backgroundColor = [UIColor colorWithPatternImage:image];
    [buttonPay addTarget:self action:@selector(OnClickLookPhone) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonPay];
    [buttonPay mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@-15);
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(image.size.width);
        make.height.mas_equalTo(image.size.height);
    }];
    
}
-(void)OnClickLookPhone{
    if ([dic[@"phone_auth"] intValue] == 0) {
        [SHJudgeTools showAlertCntrollerWithViewController:self alertControllerStyle:UIAlertControllerStyleAlert title:@"温馨提示" message:[NSString stringWithFormat:@"需%@点券可查看",dic[@"buy_service"]] CallBackBlock:^(NSInteger btnIndex) {
            
            if (btnIndex==1) {
                [self reportServiceMoblieRequest];
            }
            
        } cancelButtonTitle:@"考虑一下" destructiveButtonTitle:@"购买" otherButtonTitles:nil];
        
    }else{
        [_tableView reloadData];
    }
        
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 0;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    if (dic) {
        UILabel * titleLabel =[[UILabel alloc]initWithFrame:CGRectMake(20, 70, WIDTH -40, 20)];
        titleLabel.text  = dic[@"title"];
        titleLabel.font =[UIFont systemFontOfSize:16];
        titleLabel.numberOfLines = 0;
        [titleLabel sizeToFit];
        
        
        UILabel * contLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(titleLabel.frame)+10, WIDTH -40, 20)];
        contLabel.text  = dic[@"service_describe"];
        contLabel.font =[UIFont systemFontOfSize:15];
        contLabel.numberOfLines = 0;
        [contLabel sizeToFit];
        
        UIImageView * imageView;
        NSArray * imageArray;
        
        if ([dic[@"img_srcs"] isKindOfClass:[NSArray class]]) {
            
            imageArray = dic[@"img_srcs"];
        }
        
        if (imageArray.count>0) {
            
            for (int i=0; i<imageArray.count; i++) {
                
                imageView =[[UIImageView alloc]initWithFrame:CGRectMake(20+10*(i%3)+(WIDTH-60)/3*(i%3), CGRectGetMaxY(contLabel.frame)+10 + ((WIDTH-60)/3+10) * (i/3), (WIDTH-60)/3, (WIDTH-60)/3)];
                [imageView sd_setImageWithURL:ImageUrl(imageArray[i]) placeholderImage:ImageName(@"img_dynamic_1")];
                imageView.backgroundColor = [UIColor orangeColor];
          }
              return CGRectGetMaxY(imageView.frame)+20;
        }else {
            
             return CGRectGetMaxY(contLabel.frame)+20;
        }
    }else {
    
        return 70;
    
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * iden = @"cellid";
    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:iden];
    if (!cell) {
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:iden];
    }else{
        while ([cell.contentView.subviews lastObject] != nil) {
            
            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView * heardView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, heardHeight)];
    heardView.backgroundColor =[UIColor whiteColor];
    
    UIView * bgView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 70)];
    bgView.backgroundColor =[UIColor whiteColor];
    
    if (dic) {
        
        UIImageView * userIcon =[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
        [userIcon sd_setImageWithURL:ImageUrl(dic[@"avatar"]) placeholderImage:ImageName(@"img_default_head")];
        userIcon.layer.cornerRadius = 25.f;
        userIcon.layer.masksToBounds =YES;
        [bgView addSubview:userIcon];
        
        
        UILabel * nameLabel =[[UILabel alloc]initWithFrame:CGRectMake(70, 13, WIDTH - 90, 20)];
        nameLabel.font =[UIFont boldSystemFontOfSize:15];
        nameLabel.textColor =RGB(0x333333);
        
        if ([dic[@"phone_auth"] intValue] == 1) {
            nameLabel.text = [NSString stringWithFormat:@"%@   %@",dic[@"user_name"],dic[@"phone"]];
        }else{
           nameLabel.text = dic[@"user_name"];
        }
        
        
        
        [bgView addSubview:nameLabel];
        
        UILabel * timeLabel =[[UILabel alloc]initWithFrame:CGRectMake(70, 40, WIDTH -90, 20)];
        timeLabel.font =[UIFont systemFontOfSize:13];
        timeLabel.textColor = [UIColor grayColor];
        timeLabel.text = dic[@"update_time"];
        [bgView addSubview:timeLabel];
        
        
        UILabel * titleLabel =[[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(userIcon.frame)+10, WIDTH -40, 20)];
        titleLabel.text  = dic[@"title"];
        titleLabel.font =[UIFont systemFontOfSize:16];
        titleLabel.numberOfLines = 0;
        [titleLabel sizeToFit];
        [bgView addSubview:titleLabel];
        
        UILabel * kindLabel =[[UILabel alloc]initWithFrame:CGRectMake(WIDTH-80, 13, 65, 30)];
        kindLabel.font =[UIFont systemFontOfSize:13];
        kindLabel.textColor = strColor(@"ff7800");
        kindLabel.text = dic[@"service_type"];
        kindLabel.textAlignment = NSTextAlignmentCenter;
        kindLabel.layer.cornerRadius = 3;
        kindLabel.layer.borderWidth = 0.5;
        kindLabel.layer.borderColor = strColor(@"ff7800").CGColor;
        [bgView addSubview:kindLabel];
        
        
        UILabel * contLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(titleLabel.frame)+10, WIDTH -40, 20)];
        contLabel.text  = dic[@"service_describe"];
        contLabel.font =[UIFont systemFontOfSize:14];
        contLabel.numberOfLines = 0;
        [contLabel sizeToFit];
        [bgView addSubview:contLabel];
        
        UIImageView * imageView;
        NSArray * imageArray;
        
        if ([dic[@"img_srcs"] isKindOfClass:[NSArray class]]) {
            
            imageArray = dic[@"img_srcs"];
        }
        
        if (imageArray.count>0) {
            
            for (int i=0; i<imageArray.count; i++) {
                
                imageView =[[UIImageView alloc]initWithFrame:CGRectMake(20+10*(i%3)+(WIDTH-60)/3*(i%3), CGRectGetMaxY(contLabel.frame)+10 + ((WIDTH-60)/3+10) * (i/3), (WIDTH-60)/3, (WIDTH-60)/3)];
                [imageView sd_setImageWithURL:ImageUrl(imageArray[i]) placeholderImage:ImageName(@"img_dynamic_1")];
                imageView.backgroundColor = [UIColor orangeColor];
                imageView.userInteractionEnabled = YES;
                imageView.contentMode = UIViewContentModeScaleAspectFill;
                imageView.clipsToBounds = YES;
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageAction:)];
                [imageView addGestureRecognizer:tap];
                tap.view.tag = i;
                
                [bgView addSubview:imageView];
            }
               bgView.frame = CGRectMake(0, 0, WIDTH, CGRectGetMaxY(imageView.frame)+20);
        }else {
        
             bgView.frame = CGRectMake(0, 0, WIDTH, CGRectGetMaxY(contLabel.frame)+20);
        }

        [heardView addSubview:bgView];

    }
    return heardView;
}

//举报弹出框
-(void)roomeReportView {
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    bageView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    bageView.backgroundColor =[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5];
    [window addSubview:bageView];
    
    reportVc  = [[[NSBundle mainBundle] loadNibNamed:@"ReportNoMiddleTVC" owner:nil options:nil] lastObject];
    
    self.contentTextView = [[SZTextView alloc]initWithFrame:CGRectMake(10, 60, 260, 95)];
    self.contentTextView.font = [UIFont systemFontOfSize:15];
    self.contentTextView.placeholder = @"请输入举报内容";
    [reportVc.bgView addSubview:self.contentTextView];
    
    reportVc.center = CGPointMake(bageView.center.x, self.view.center.y-64);
    
    [reportVc.sureBtn addTarget:self action:@selector(alertSureBtn:) forControlEvents:UIControlEventTouchUpInside];
    [reportVc.closeBtn addTarget:self action:@selector(alertCloseBtn:) forControlEvents:UIControlEventTouchUpInside];
    [bageView addSubview:reportVc];
    
}

-(void)alertSureBtn:(UIButton *)btn{
    
   [bageView removeFromSuperview];
    if(self.contentTextView.text.length==0||self.contentTextView.text==nil){
    
        SHuiMBhudTextTimeStr(@"请输入举报内容");
        return;
    }else {
        
        [self reportMessageDetailsRequest];
    }
}

-(void)alertCloseBtn:(UIButton *)btn{
    
    [bageView removeFromSuperview];
}

- (void)tapImageAction:(UITapGestureRecognizer *)tap {
    
    NSArray  * imageArray;
    if ([dic[@"img_srcs"] isKindOfClass:[NSArray class]]) {
        
       imageArray  = dic[@"img_srcs"];
    }
    
    TCLPhotoBrowserViewController *photo = [[TCLPhotoBrowserViewController alloc]init];
    photo.photos = imageArray;
    photo.currentPage = tap.view.tag;
    [self presentViewController:photo animated:YES completion:nil];
}

-(void)getDetailsRequest{
    
    isUser
    if (!user.user_id) {
        user.user_id = @"";
    }
    NSDictionary * datadic = @{@"service_id":self.severID,
                               @"user_id":user.user_id
                           };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(BuildService_Details_api) andparameters:datadic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            
            dic = responseObject[@"datas"];
            
            if ([dic[@"phone_auth"] intValue] == 1) {
                [buttonPay removeFromSuperview];
            }
            
            if (!dic[@"service_id"]) {
                [self.navigationController popViewControllerAnimated:YES];
            }
        }else {
        
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        [_tableView reloadData];
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextNetError;
    }];
}

-(void)reportMessageDetailsRequest{
    
    isUser
    
//    1、招聘，2、建企服务，3、简历
    NSDictionary * datadic = @{@"ser_id":self.severID,
                               @"user_id":user.user_id,
                               @"token":user.token,
                               @"type":@"2",
                               @"content":self.contentTextView.text
                               };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(Report_request_api) andparameters:datadic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            
            SHuiMBhudTextTimeStr(@"举报成功");
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
//        [_tableView reloadData];
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextNetError;
    }];
}

-(void)reportServiceMoblieRequest{
    
    isUser
    
    NSDictionary * datadic = @{@"service_id":self.severID,
                               @"user_id":user.user_id,
                               @"token":user.token};
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(buy_service_Details_api) andparameters:datadic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            SHuiMBhudTextTimeStr(@"购买成功");
            [self getDetailsRequest];
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
            
        }
        //        [_tableView reloadData];
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextNetError;
    }];
}
@end
